package cn.edu.gsau.info.controller;


import cn.edu.gsau.info.models.Code;
import cn.edu.gsau.info.models.Keyan;
import cn.edu.gsau.info.models.ResponseResult;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.service.IKeyanService;
import cn.edu.gsau.info.util.UploadUtil;
import cn.edu.gsau.info.vo.Params;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jinjiang
 * @since 2022-11-17
 */
@RestController
@RequestMapping("/keyan")
@JsonSerialize(using = ToStringSerializer.class)
public class KeyanController {

    @Value("${gsau.upload.path}")
    private String path;


    @Autowired
    private IKeyanService keyanService;

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Keyan keyan){
        keyanService.addInfo(keyan);
        return new ResponseResult(Code.SAVE_OK ,"添加成功", null);
    }

    @PostMapping("/upload")
    public ResponseResult upload(@RequestBody MultipartFile file){
        UploadUtil upload = new UploadUtil();
        Map<String , String> map = upload.filedUpload(file , "101" ,path);
        return new ResponseResult(map == null?Code.SAVE_ERR:Code.SAVE_OK , map == null?"文件上传失败":"文件上传成功" , map);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        boolean delete = keyanService.removeById(id);
        return new  ResponseResult(delete == true?Code.DELETE_OK:Code.DELETE_ERR , delete == true?"删除成功":"删除失败" ,null);    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Keyan keyan){
        boolean update = keyanService.updateById(keyan);
        return new  ResponseResult(update == true?Code.UPDATE_OK:Code.UPDATE_ERR , update == true?"修改成功":"修改失败" ,null);
    }

    @GetMapping("/find/{id}")
    public ResponseResult findById(@PathVariable Long id){
        User user = new User();
        user.setId(1L);
        List<Keyan> list = keyanService.selectById(user);
        return new  ResponseResult(list == null?Code.GET_ERR:Code.GET_OK , list == null?"查询失败":"查询成功" , list);
    }
    @GetMapping("findall")
    public ResponseResult findAll(){
        List<Keyan> list =keyanService.selectAll();
        return new  ResponseResult(list == null?Code.GET_ERR:Code.GET_OK , list == null?"查询失败":"查询成功" , list);
    }
    @PostMapping("selectbycondation")
    public ResponseResult<Keyan> selectbycondation(@RequestBody Params params){
        System.out.println(params);
        List<Keyan> list = keyanService.selecteByCondation(params) ;
        return new ResponseResult(list == null?Code.GET_ERR:Code.GET_OK , list == null?"查询失败":"查询成功" , list);
    }
}

